package com.xiaoyu.lingdian.controller;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.xiaoyu.lingdian.tool.DateUtil;
import com.xiaoyu.lingdian.tool.RandomUtil;
import com.xiaoyu.lingdian.tool.StringUtil;
import javax.servlet.http.HttpServletResponse;
import com.xiaoyu.lingdian.core.mybatis.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import com.xiaoyu.lingdian.tool.out.ResultMessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import com.xiaoyu.lingdian.controller.BaseController;
import com.xiaoyu.lingdian.entity.CoreAttachment;
import com.xiaoyu.lingdian.entity.CoreMyButler;
import com.xiaoyu.lingdian.service.CoreAttachmentService;
import com.xiaoyu.lingdian.service.CoreMyButlerService;
import com.xiaoyu.lingdian.vo.CoreMyButlerVO;

@Controller
@RequestMapping(value="/coreMyButler")
public class CoreMyButlerController extends BaseController {

	/**
	* CoreMyButler表
	*/
	@Autowired
	private CoreMyButlerService coreMyButlerService;
	
	/**
	 * 附件表
	 */
	@Autowired
	private CoreAttachmentService coreAttachmentService;
	
	/**
	* 添加
	*
	* @param crmbrName 名字
	* @param crmbrTel 电话号码
	* @param crmbrQq QQ
	* @param uuids 附件ID
	* @return
	*/
	@RequestMapping(value="/add/coreMyButler", method=RequestMethod.POST)
	public void addCoreMyButler (String crmbrName, String crmbrTel, String crmbrQq, String uuids, HttpServletResponse response) {
		logger.info("[CoreMyButlerController]:begin addCoreMyButler");

		if (StringUtil.isEmpty(crmbrName)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[名字]不能为空!"), response);
			return;
		}
		if (StringUtil.isEmpty(crmbrTel)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[电话号码]不能为空!"), response);
			return;
		}
		if (StringUtil.isEmpty(crmbrQq)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[QQ]不能为空!"), response);
			return;
		}		CoreMyButler coreMyButler = new CoreMyButler();
		String uuid = RandomUtil.generateString(16);
		coreMyButler.setCrmbrUuid(uuid);
		coreMyButler.setCrmbrName(crmbrName);
		coreMyButler.setCrmbrTel(crmbrTel);
		coreMyButler.setCrmbrQq(crmbrQq);
		coreMyButler.setCrmbrCdate(new Date());
		coreMyButler.setCrmbrUdate(new Date());

		coreMyButlerService.insertCoreMyButler(coreMyButler);
		// 添加在线客服头像
		if (!StringUtils.isBlank(uuids)) {
			CoreAttachment coreAttachment = new CoreAttachment();
			String[] uuidModel = uuids.split("\\|");
			for (int i = 0; i < uuidModel.length; i++) {
				coreAttachment = new CoreAttachment();
				coreAttachment.setCratmUuid(uuidModel[i]);
				coreAttachment.setCratmBusUuid(uuid);
				coreAttachmentService.updateCoreAttachment(coreAttachment);
			}
		}
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "新增成功!"),response);
		logger.info("[CoreMyButlerController]:end addCoreMyButler");

	}

	/**
	* 修改
	*
	* @param crmbrName 名字
	* @param crmbrTel 电话号码
	* @param crmbrQq QQ
	* @param uuids 附件ID
	* @return
	*/
	@RequestMapping(value="/update/coreMyButler", method=RequestMethod.POST)
	public void updateCoreMyButler (String crmbrUuid, String crmbrName, String crmbrTel, String crmbrQq, String uuids, HttpServletResponse response) {
		logger.info("[CoreMyButlerController]:begin updateCoreMyButler");
		if (StringUtil.isEmpty(crmbrUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		if (StringUtil.isEmpty(crmbrName)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[名字]不能为空!"), response);
			return;
		}
		if (StringUtil.isEmpty(crmbrTel)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[电话号码]不能为空!"), response);
			return;
		}
		if (StringUtil.isEmpty(crmbrQq)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[QQ]不能为空!"), response);
			return;
		}
		CoreMyButler coreMyButler = new CoreMyButler();
		coreMyButler.setCrmbrUuid(crmbrUuid);
		coreMyButler.setCrmbrName(crmbrName);
		coreMyButler.setCrmbrTel(crmbrTel);
		coreMyButler.setCrmbrQq(crmbrQq);
		coreMyButler.setCrmbrUdate(new Date());

		coreMyButlerService.updateCoreMyButler(coreMyButler);
		// 修改在线客服头像
		if (!StringUtils.isBlank(uuids)) {
			CoreAttachment coreAttachment = new CoreAttachment();
			String[] uuidModel = uuids.split("\\|");
			for (int i = 0; i < uuidModel.length; i++) {
				coreAttachment = new CoreAttachment();
				coreAttachment.setCratmUuid(uuidModel[i]);
				coreAttachment.setCratmBusUuid(crmbrUuid);
				coreAttachmentService.updateCoreAttachment(coreAttachment);
			}
		}
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "修改成功!"),response);
		logger.info("[CoreMyButlerController]:end updateCoreMyButler");

	}

	/**
	* 删除
	*
	* @param crmbrUuid 标识UUID
	* @return
	*/
	@RequestMapping(value="/delete/one", method=RequestMethod.POST)
	public void deleteCoreMyButler (String crmbrUuid, HttpServletResponse response) {
		logger.info("[CoreMyButlerController]:begin deleteCoreMyButler");
		if (StringUtil.isEmpty(crmbrUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		CoreMyButler coreMyButler = new CoreMyButler();
		coreMyButler.setCrmbrUuid(crmbrUuid);

		coreMyButlerService.deleteCoreMyButler(coreMyButler);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "删除成功!"),response);
		logger.info("[CoreMyButlerController]:end deleteCoreMyButler");

	}

	/**
	* 批量删除
	*
	* @param crmbrUuids UUID集合
	* @return
	*/
	@RequestMapping(value="/delete/batch", method=RequestMethod.POST)
	public void deleteBatchCoreMyButler (String crmbrUuids, HttpServletResponse response) {
		logger.info("[CoreMyButlerController]:begin deleteBatchCoreMyButler");
		if (StringUtil.isEmpty(crmbrUuids)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[UUID集合]不能为空!"), response);
			return;
		}
		String[] uuids=crmbrUuids.split("\\|");
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < uuids.length; i++) {
			list.add(uuids[i]);
		}
		if (list.size() == 0) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "请选择批量删除对象！"), response);
			return;
		}
		coreMyButlerService.deleteBatchByIds(list);

		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "批量删除成功!"),response);
		logger.info("[CoreMyButlerController]:end deleteBatchCoreMyButler");

	}

	/**
	* 获取单个
	*
	* @param crmbrUuid 标识UUID
	* @return
	*/
	@RequestMapping(value="/views", method=RequestMethod.POST)
	public void viewsCoreMyButler (String crmbrUuid, HttpServletResponse response) {
		logger.info("[CoreMyButlerController]:begin viewsCoreMyButler");
		if (StringUtil.isEmpty(crmbrUuid)) {
			writeAjaxJSONResponse(ResultMessageBuilder.build(false, -1, "[标识UUID]不能为空!"), response);
			return;
		}
		CoreMyButler coreMyButler = new CoreMyButler();
		coreMyButler.setCrmbrUuid(crmbrUuid);

		coreMyButler = coreMyButlerService.getCoreMyButler(coreMyButler);

		CoreMyButlerVO coreMyButlerVO = new CoreMyButlerVO();
		coreMyButlerVO.convertPOToVO(coreMyButler);
		
		//根据在线客服UUID查询图片附件
		List<CoreAttachment> attachmentslist = coreAttachmentService.findCoreAttachmentByCnd(crmbrUuid);
		if (attachmentslist != null && attachmentslist.size() > 0) {
			coreMyButlerVO.setList(attachmentslist);
		}
				writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "获取单个信息成功", coreMyButlerVO), response);
		logger.info("[CoreMyButlerController]:end viewsCoreMyButler");

	}

	/**
	* 获取列表<List>	
	* @return
	*/
	@RequestMapping(value="/find/all", method=RequestMethod.POST)
	public void findCoreMyButlerList (HttpServletResponse response) {
		logger.info("[CoreMyButlerController]:begin findCoreMyButlerList");
		List<CoreMyButler> lists = coreMyButlerService.findCoreMyButlerList();
		List<CoreMyButlerVO> vos = new ArrayList<CoreMyButlerVO>();
		CoreMyButlerVO vo;
		for (CoreMyButler coreMyButler : lists) {
			vo = new CoreMyButlerVO();
			vo.setCrmbrUuid(coreMyButler.getCrmbrUuid());
			vo.setCrmbrName(coreMyButler.getCrmbrName());
			vo.setCrmbrTel(coreMyButler.getCrmbrTel());
			vo.setCrmbrQq(coreMyButler.getCrmbrQq());
			vo.setCrmbrCdate(DateUtil.formatTimesTampDate(coreMyButler.getCrmbrCdate()));
			vo.setCrmbrUdate(DateUtil.formatTimesTampDate(coreMyButler.getCrmbrUdate()));
			//根据在线客服UUID查询图片附件
			List<CoreAttachment> attachmentslist = coreAttachmentService.findCoreAttachmentByCnd(coreMyButler.getCrmbrUuid());
			if (attachmentslist != null && attachmentslist.size() > 0) {
				vo.setList(attachmentslist);
			}
						vos.add(vo);
		}
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "list列表获取成功!", vos),response);
		logger.info("[CoreMyButlerController]:end findCoreMyButlerList");

	}

	/**
	* 获取列表<Page>	
	* @param pageNum 页码
	* @param pageSize 页数
	* @param crmbrName 名字
	* @return
	*/
	@RequestMapping(value="/find/by/cnd", method=RequestMethod.POST)
	public void findCoreMyButlerPage (Integer pageNum, Integer pageSize, String crmbrName, HttpServletResponse response) {
		logger.info("[CoreMyButlerController]:begin findCoreMyButlerPage");
		if (pageNum == null || pageNum == 0) {
			pageNum = 1;
		}
		if (pageSize == null || pageSize == 0) {
			pageSize = 10;
		}
		Page<CoreMyButler> page = coreMyButlerService.findCoreMyButlerPage(pageNum, pageSize, crmbrName);
		Page<CoreMyButlerVO> pageVO = new Page<CoreMyButlerVO>(page.getPageNumber(), page.getPageSize(), page.getTotalCount());
		List<CoreMyButlerVO> vos = new ArrayList<CoreMyButlerVO>();
		CoreMyButlerVO vo;
		for (CoreMyButler coreMyButler : page.getResult()) {
			vo = new CoreMyButlerVO();
			vo.setCrmbrUuid(coreMyButler.getCrmbrUuid());
			vo.setCrmbrName(coreMyButler.getCrmbrName());
			vo.setCrmbrTel(coreMyButler.getCrmbrTel());
			vo.setCrmbrQq(coreMyButler.getCrmbrQq());
			vo.setCrmbrCdate(DateUtil.formatTimesTampDate(coreMyButler.getCrmbrCdate()));
			vo.setCrmbrUdate(DateUtil.formatTimesTampDate(coreMyButler.getCrmbrUdate()));
			//根据在线客服UUID查询图片附件
			List<CoreAttachment> attachmentslist = coreAttachmentService.findCoreAttachmentByCnd(coreMyButler.getCrmbrUuid());
			if (attachmentslist != null && attachmentslist.size() > 0) {
				vo.setList(attachmentslist);
			}
						vos.add(vo);
		}
		pageVO.setResult(vos);
		writeAjaxJSONResponse(ResultMessageBuilder.build(true, 1, "page列表获取成功!", pageVO),response);
		logger.info("[CoreMyButlerController]:end findCoreMyButlerPage");

	}
//<=================定制内容开始==============
//==================定制内容结束==============>

}